package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.respository.DestinationRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository destinationRepository;
    @Autowired
    private IRegionService regionService;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public List<Destination> queryDestById(String rid) {
        // 找到区域
        Region region = regionService.get(rid);
        // 根据区域下的关联目的地id数组
        List<String> refIds = region.getRefIds();

        // 查询目的地
        return destinationRepository.findByIdIn(refIds);
    }

    @Override
    public Page<Destination> query(DestinationQuery qo) {

        // MQL拼接语句
        Query query = new Query();

        // 条件
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }

        // 分页对象 传入分页需要的参数 当前页-1  每页行数  排序规则  排序依据
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");

        // 返回分页对象
        // Page 是接口
        // PageImpl 是实现类
        return DBHelper.query(template, Destination.class, query, pageable);
    }

    @Override
    public void saveOrUpdate(Destination destination) {

        if (StringUtils.hasText(destination.getId())) {

            this.update(destination);
        } else {
            this.save(destination);
        }

    }

    @Override
    public List<Destination> queryToasts(String id) {
        List<Destination> list = new ArrayList<>();

        // 递归调用
        this.createToast(list, id);

        // list集合元素顺序调转
        Collections.reverse(list);

        return list;
    }
    private void createToast(List<Destination> list, String parentId) {

        // 递归前提 出口
        if (!StringUtils.hasLength(parentId)) {
            return;
        }

        Destination destination = this.get(parentId);
        list.add(destination);

        if (StringUtils.hasLength(destination.getParentId())) {
            createToast(list, destination.getParentId());
        }

    }

    @Override
    public List<Destination> queryDestByRegionIdForApi(String rid) {

        List<Destination> list = new ArrayList<>();

        // 查询区域的子目的地
        if ("-1".equals(rid)) {
            // 说明是国内
            list = destinationRepository.findByParentName("中国");
        } else {
            list = queryDestById(rid);
        }

        // 查询子目的地下面的子目的地
        // 获取子元素
        for (Destination destination : list) {
            //jpa 查询方法可以添加分页, 排序操作
            Pageable pageable =
                    PageRequest.of(0, 5, Sort.Direction.ASC, "_id");
            List<Destination> children = destinationRepository.findByParentId(destination.getId(), pageable);
            destination.setChildren(children);
        }

        return list;
    }

    @Override
    public List<Destination> findByDeep(int deep) {
        return destinationRepository.findByDeep(deep);
    }

    @Override
    public void updateInfo(Destination destination) {
        // 先查询到原本的数据
        Destination dest = this.get(destination.getId());
        // 保存简介
        dest.setInfo(destination.getInfo());
        // 保存数据
        this.update(dest);

    }

    @Override
    public Destination queryByName(String name) {
        return destinationRepository.queryByName(name);
    }

}


































































